﻿using System;


namespace myLittleC
{
    class Program
    {
        static void About() 
        {
            Console.WriteLine("my Little C Language Compiler v0.2");
            Console.WriteLine("Author : Oleg \"Corwin\" Pinchuk, 236gr.");
            Console.WriteLine("Syntax : myLittleC -[l|px|ts|fpx|cg|op1|op2|cgop] source");
            Console.WriteLine("   -l  start token analyzer");
            Console.WriteLine("   -px  start parsing expressions");
            Console.WriteLine("   -ts  write Symbol Table");
        }
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    About();
                    return;
                }

                switch (args[0])
                {
                    case "-l":
                        Tokenizer tokenizer = new Tokenizer(args[1]);
                        for (Token nToken = tokenizer.ActiveToken;
                             TokenType.EOF != nToken.Type;
                             nToken = tokenizer.GetNextToken())
                            Console.WriteLine(nToken.ToString());
                        break;
                    case "-px":
                        var s = new Parser(args[1]);
                        s.Parse();
                        Console.WriteLine(s.ToString());
                        break;
                    case "-ts":
                        ParserWithStatement e = new ParserWithStatement(args[1]);
                        e.Parse();
                        Console.WriteLine(e.WriteTable());
                        break;
                    case "-fpx":
                        ParserWithStatement r = new ParserWithStatement(args[1]);
                        r.Parse();
                        Console.WriteLine(r.ToString());
                        break;
                    case "-op2":
                        ParserWithStatement rrr = new ParserWithStatement(args[1]);
                        rrr.Parse();
                        rrr.Optimise();
                        Console.WriteLine(rrr.ToString());
                        break;
                    case "-cg":
                        ParserWithStatement rr = new ParserWithStatement(args[1]);
                        rr.Parse();
                        CodeGenerator cg = new CodeGenerator(rr);
                        
                        cg.Generate();
                        Console.WriteLine(cg.ToString());
                        break;
                    case "-op1":
                        var opparser = new ParserWithStatement(args[1]);
                        opparser.Parse();
                        var opgen = new CodeGenerator(opparser, Optimisers.LowLavel);

                        opgen.Generate();
                        Console.WriteLine(opgen.ToString());
                        break;
                    default:
                        About();
                        break;
                }
            }
            catch (ComException ex)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
